﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AJAXServiceProxyGeneration;
using System.IO;
using System.Security.Permissions;
using System.Xml.Serialization;
using System.Xml;

namespace AJAXCodeGenerator
{   
    public partial class Main : Form, IGeneratorWriter
    {
        private Generator.IGeneratorControl GeneratorControl
        {
            get { return (Generator.IGeneratorControl) this.pnlGenerator.Controls[0]; }
        }

        public Main()
        {
            InitializeComponent();

            #if DEBUG
            this.txtPOCOClassOutputDirectory.Text = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "../../../Sample.ClientScripts/Services");
            #endif
            //Add Default UserControl
            Generator.ScriptSharpRefector uc = new Generator.ScriptSharpRefector();
            uc.Dock = DockStyle.Fill;
            this.pnlGenerator.Controls.Add(uc);

            try
            {
                
                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                using (FileStream reader = File.OpenRead(this.SaveDirectory))
                {
                    Settings settings = (Settings) serializer.Deserialize(reader);
                    this.txtPOCOClassOutputDirectory.Text = settings.POCOClassSaveDirectory;
                    this.txtServiceClassOutputDirectory.Text = settings.ServiceClassSaveDirectory;
                    this.GeneratorControl.LoadSettings(settings.GeneratorSettings);
                }
            }
            catch
            {
            }
           
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            try
            {
                this.lvFiles.Items.Clear();
                this.rtbCode.Text = "Generating Preview Files";
                this.Refresh();
                IGenerator generator = this.GeneratorControl.GetGenerator();
                generator.POCOClassGeneratorWriter = this;
                generator.ServiceClassGeneratorWriter = this;
                generator.Generate();

                this.lvFiles.Sorting = SortOrder.Ascending;
                this.lvFiles.Sort();

                this.rtbCode.Text += Environment.NewLine + "Generation Successful";

                
            }
            catch (Exception ex)
            {
                this.rtbCode.Text = ex.ToString();
            }

            this.ShowResults();
        }
        private void ShowResults()
        {
            this.tabMain.SelectTab(this.tabResults);            
        }

        private void lvFiles_ItemActivate(object sender, EventArgs e)
        {
            this.rtbCode.Text = this.lvFiles.SelectedItems[0].Tag.ToString();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                this.txtPOCOClassOutputDirectory.Text = folderBrowserDialog.SelectedPath;
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                this.rtbCode.Text = "Generating Preview Files";
                this.Refresh();
                IGenerator generator = this.GeneratorControl.GetGenerator();
                generator.ServiceClassGeneratorWriter = new GeneratorFileWriter(this.txtServiceClassOutputDirectory.Text);
                generator.POCOClassGeneratorWriter = new GeneratorFileWriter(this.txtPOCOClassOutputDirectory.Text);
                generator.Generate();

                this.rtbCode.Text += Environment.NewLine + "Generation Successful";
            }
            catch (Exception ex)
            {
                this.rtbCode.Text = ex.ToString();
            }

            this.ShowResults();
        }
        private string SaveDirectory
        {
            get
            {
                return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Settings.xml");
            }
        }
        private void btnSaveSettings_Click(object sender, EventArgs e)
        {
            try
            {
                Settings settings = new Settings();
                settings.GeneratorSettings = this.GeneratorControl.GetSettings();
                settings.POCOClassSaveDirectory = this.txtPOCOClassOutputDirectory.Text;
                settings.ServiceClassSaveDirectory = this.txtServiceClassOutputDirectory.Text;
                XmlSerializer serializer = new XmlSerializer(typeof(Settings));
                using (XmlWriter writer = XmlWriter.Create(this.SaveDirectory))
                {
                    serializer.Serialize(writer, settings);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), ex.Message);
            }

        }

        #region IGeneratorWriter Members

       
        public void Write(string fileName, System.IO.Stream fileStream)
        {
            fileStream.Seek(0, 0);

            using (StreamReader sr = new StreamReader(fileStream))
            {
                ListViewItem lvi = new ListViewItem(fileName);
                lvi.Tag = sr.ReadToEnd();
                this.lvFiles.Items.Add(lvi);
            }
            
        }

        #endregion

        private void tabSettings_Click(object sender, EventArgs e)
        {

        }

        private void btnBrowseServiceClass_Click(object sender, EventArgs e)
        {
            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
            {
                this.txtServiceClassOutputDirectory.Text = folderBrowserDialog.SelectedPath;
            }
        }
    }
}
